//
//  ActiveViewController.m
//  moon
//
//  Created by beyself on 16/6/25.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "ActiveViewController.h"
#import "SetPwdViewController.h"

@interface ActiveViewController ()<UIActionSheetDelegate>

@end

@implementation ActiveViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"请确认您的身份";
    
    _mChoose = [[NSMutableDictionary alloc] init];
    _mIdentityArray = [[NSMutableArray alloc] init];
    for(NSDictionary* item in _mIdentitylabel)
    {
        if([item[@"type"] intValue] == 0)
        {
            [_mIdentityArray addObject:item];
        }
    }
    
    UIScrollView* scrollview = [[UIScrollView alloc] init];
    scrollview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 96);
    scrollview.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollview];
    
    int yPos = 10;
    for(int i = 0; i < 2; i++)
    {
        UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 48);
        button.titleLabel.font = FONT_15;
        button.enabled = NO;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button setTitleColor:BLUE forState:UIControlStateDisabled];
        [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateDisabled];
        
        UILabel* label = [[UILabel alloc] init];
        label.frame = CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2 - 10, 48);
        label.font = FONT_15;
        label.textColor = BLACK;
        label.textAlignment = NSTextAlignmentRight;
        [button addSubview:label];
        
        if(i == 0)
        {
            [button setTitle:@"您的姓名" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_man"] forState:UIControlStateNormal];
            label.text = _mMember[@"username"];
        }
        else if(i == 1)
        {
            [button setTitle:@"您的电话" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"icon_phone"] forState:UIControlStateNormal];
            label.text = _mMember[@"telephone"];
        }
        [scrollview addSubview:button];
        
        CALayer* layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
        layer.backgroundColor = BG.CGColor;
        [button.layer addSublayer:layer];
        
        yPos += 48;
    }
    
    yPos += 10;
    NSArray* array = _mMember[@"classes"];
    if(array.count)
    {
        for(int i = 0; i < array.count + 1; i++)
        {
            UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 48);
            button.titleLabel.font = FONT_15;
            button.enabled = NO;
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [button setTitleColor:BLUE forState:UIControlStateDisabled];
            [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateDisabled];
            if(i == 0) [button setTitle:@"所在班级" forState:UIControlStateDisabled];
            else
            {
                NSDictionary* item = array[i - 1];
                [button setTitle:[NSString stringWithFormat:@"%@%@",item[@"school_name"],item[@"class_name"]] forState:UIControlStateDisabled];
            }
            [scrollview addSubview:button];
            
            CALayer* layer = [[CALayer alloc] init];
            layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
            layer.backgroundColor = BG.CGColor;
            [button.layer addSublayer:layer];

            yPos += 48;
        }
    }
    
    yPos += 10;
    array = _mMember[@"children"];
    if(array.count)
    {
        for(int i = 0; i < array.count + 1; i++)
        {
            UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 48);
            button.titleLabel.font = FONT_15;
            button.enabled = NO;
            button.tag = 2000 + i;
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [button setTitleColor:BLUE forState:UIControlStateDisabled];
            [button setTitleColor:BLUE forState:UIControlStateNormal];
            [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateDisabled];
            [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
            [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(showIdentify:) forControlEvents:UIControlEventTouchUpInside];
            if(i == 0) [button setTitle:@"您的小孩" forState:UIControlStateDisabled];
            else
            {
                button.enabled = YES;
                NSDictionary* item = array[i - 1];
                [button setTitle:item[@"name"] forState:UIControlStateNormal];
                
                UILabel* label = [[UILabel alloc] init];
                label.frame = CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2 - 30, 48);
                label.font = FONT_15;
                label.tag = 100;
                label.textColor = BLACK;
                label.textAlignment = NSTextAlignmentRight;
                [button addSubview:label];
                
                UIButton* arrow = [UIButton buttonWithType:UIButtonTypeCustom];
                arrow.frame = CGRectMake(SCREEN_WIDTH - 32, 0, 32, 48);
                [arrow setImage:[UIImage imageNamed:@"ic_arrow"] forState:UIControlStateNormal];
                [button addSubview:arrow];
            }
            [scrollview addSubview:button];
            
            CALayer* layer = [[CALayer alloc] init];
            layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
            layer.backgroundColor = BG.CGColor;
            [button.layer addSublayer:layer];
            
            yPos += 48;
        }
    }
    
    scrollview.contentSize = CGSizeMake(SCREEN_WIDTH, yPos);
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, SCREEN_HEIGHT - 54, SCREEN_WIDTH - 20, 44);
    button.layer.cornerRadius = 22;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[Utils imageFromColor:ORANGE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"下一步" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, SCREEN_HEIGHT - 86, SCREEN_WIDTH - 20, 32);
    button.titleLabel.font = FONT_10;
    [button setTitleColor:BLACK forState:UIControlStateNormal];
    [button setTitle:@"如您发现身份有误，请拨打客服电话800-8486995" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(makeCall) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void) makeCall
{
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"8008486995"];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex != actionSheet.cancelButtonIndex)
    {
        UILabel* label = (UILabel*)[[self.view viewWithTag:2000 + _mSelected] viewWithTag:100];
        label.text = _mIdentityArray[buttonIndex - 1][@"name"];
        
        NSArray* array = _mMember[@"children"];
        NSString* identify = [_mIdentityArray[buttonIndex - 1][@"id"] description];
        NSString* child = [array[_mSelected - 1][@"student_id"] description];
        
        [_mChoose setValue:identify forKey:child];
    }
}

- (void) showIdentify:(UIButton*)btn
{
    _mSelected = (int)btn.tag - 2000;
    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"返回" destructiveButtonTitle:nil otherButtonTitles:nil, nil];

    for(NSDictionary* item in _mIdentityArray)
    {
        [sheet addButtonWithTitle:item[@"name"]];
    }
    
    [sheet showInView:self.view];
}

- (void) confirm
{
    NSArray* array = _mMember[@"children"];
    if([_mChoose allKeys].count < array.count)
    {
        [self showMsg:@"请为每一位孩子选择身份"];
        return;
    }
    
    SetPwdViewController* subview = [[SetPwdViewController alloc] init];
    subview.mUseId = [_mMember[@"id"] description];
    subview.mParam = _mChoose;
    [self.navigationController pushViewController:subview animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
